#include "xgstring.h"

xgstring::xgstring()
{
}

int xgstring::kmp(QString s,QString a, int i){
    int l1 = s.length(), l2 = a.length();
    //qDebug("l1 = %d, l2 = %d",l1,l2);
    int p = 0, j = 0;
    while(i + p < l1 && j < l2){
        //qDebug("i = %d, p = %d, j = %d",i,p,j);
        if(s[i + p] == a[j]){
            ++p;
            ++j;
        }
        else{
            i ++;
            p = 0;
            j = 0;
        }
        if(j == l2) break;
    }
    if(j == l2)return i;
    else return -1;
}

QString xgstring::getField(QString s, QString begin, QString end, int k){
    int i = kmp(s, begin, k);
    int j = kmp(s, end, k);
    if(i != -1 && j != -1)
    return s.mid(i + begin.length(), j - i - begin.length());
    else return "";
}
QString xgstring::getField(QString s, QString begin, int k){
    int i = kmp(s, begin, k);
    if(i != -1)
    return s.right(s.length() - i - begin.length());
    else return "";
}
QString xgstring::simplify(QString in){
    //qDebug("begin simplify");
    int pos = 0, j = 0;
    QString out;
    in = ">" + in + "<";
    //qDebug() << in;
    while((pos = kmp(in,">",pos)) != -1){
        j = kmp(in, "<", pos);
        if(j - pos > 1)out = out + in.mid(pos + 1, j - pos - 1);
        pos = j;

    }
    //qDebug("OK");
    return out;
}

QString xgstring::changetime(QString in){
    QString out;
    int k;
    if(kmp(in, QApplication::trUtf8("刚")) != -1
       || kmp(in, QApplication::trUtf8("小时")) != -1 ||
       kmp(in, QApplication::trUtf8("分钟")) != -1
       ){
        out = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm");
    }
    else{
        if((k = kmp(in, QApplication::trUtf8("年")))  != -1)
            out += in.left(4) + "-" + getField(in, QApplication::trUtf8("年"), QApplication::trUtf8("月")) + "-";
        else if(kmp(in, QApplication::trUtf8("今天")) != -1){
            out += QDate::currentDate().toString("yyyy-MM-dd");
        }
        else if(kmp(in, QApplication::trUtf8("昨天")) != -1){
            QDate t = QDate::fromJulianDay(QDate::currentDate().toJulianDay() - 1);
            out += QString::number(t.year()) + "-"
                   + QString::number(t.month()) +"-"
                   + QString::number(t.day());
        }
        else {
            k = kmp(in, QApplication::trUtf8("月"));
            out += "2011-" + in.left(k) + "-";
        }
        out += getField(in, QApplication::trUtf8("月"), QApplication::trUtf8("日"));
        out = QDate::fromString(out, "yyyy-M-d").toString("yyyy-MM-dd");
        out += in.right(6);
    }

    return out;
}

int xgstring::getzhnum(QString in){
    if(kmp(in, QApplication::trUtf8("一")) != -1 || kmp(in, "Mon") != -1)
        return 1;
    if(kmp(in, QApplication::trUtf8("二")) != -1 || kmp(in, "Tue") != -1)
        return 2;
    if(kmp(in, QApplication::trUtf8("三")) != -1 || kmp(in, "Wed") != -1)
        return 3;
    if(kmp(in, QApplication::trUtf8("四")) != -1 || kmp(in, "Thu") != -1)
        return 4;
    if(kmp(in, QApplication::trUtf8("五")) != -1 || kmp(in, "Fri") != -1)
        return 5;
    if(kmp(in, QApplication::trUtf8("六")) != -1 || kmp(in, "Sat") != -1)
        return 6;
    if(kmp(in, QApplication::trUtf8("日")) != -1 || kmp(in, "Sun") != -1)
        return 7;
}
